<?php

class QuestionForm extends BaseForm {
  const FORM_NAME           = 'frm';

  const FIELD_COURSE      = 'course';
  const FIELD_QUESTION    = 'question';
  const FIELD_STATUS      = 'status';

  public function configure() {
    $arrStatusList    = StringUtil::getStatusArray();
    $arrCourseList    = COURSETable::getInstance()->getCoursesArray();

    $widget = array(
            self::FIELD_COURSE      => new sfWidgetFormSelect(array('choices' => $arrCourseList), array('multiple' => false, 'expand' => true)),
            self::FIELD_QUESTION    => new sfWidgetFormInputText(array(), array('maxlength' => 255)),
            self::FIELD_STATUS      => new sfWidgetFormSelect(array('choices' => $arrStatusList), array('multiple' => false, 'expand' => true)),
    );

    $labels = array(
            self::FIELD_COURSE   => 'Course<req />',
            self::FIELD_QUESTION    => 'Question<req />',
            self::FIELD_STATUS         => 'Status<req />'
    );

    $this->setWidgets($widget);
    $this->widgetSchema->setLabels($labels);

    $validators = array(
            self::FIELD_COURSE   => new sfValidatorString(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_COURSE])))),
            self::FIELD_QUESTION   => new sfValidatorString(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_QUESTION])))),
            self::FIELD_STATUS      => new sfValidatorString(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_STATUS]))))
    );

    $this->setValidators($validators);
    $this->validatorSchema->setPostValidator(new sfValidatorCallback(array('callback' => array($this, 'postValidator'))));

    $this->widgetSchema->setNameFormat(self::FORM_NAME . '[%s]');

    $this->validatorSchema->setOption('allow_extra_fields', true);
    $this->validatorSchema->setOption('filter_extra_fields', false);
  }

  public function postValidator($validator, $values) {
    return $values;
  }

  public function populateData(QUESTION $question) {
    $this->setDefault(self::FIELD_COURSE, $question->getCourseId());
    $this->setDefault(self::FIELD_QUESTION, $question->getQuestion());
    $this->setDefault(self::FIELD_STATUS, $question->getIsActive());
  }
}
?>